//
//  GoodsOrderListCtr.swift
//  Aiwei
//
//  Created by zhu xietong on 2017/11/10.
//  Copyright © 2017年 zhu xietong. All rights reserved.
//

import Foundation
import Eelay
import soapp

import AsyncDisplayKit



open class SaleAfterCtr:ASTableContainer{
    public required init() {
        super.init()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open override func viewDidLoad() {
        self.content = ASTermsContainer.list(
               net: JoTask().post._surl(.售后列表).log_response(),
               node: "list",
               emptyMsg: "没有相关内容".loc,
               params: nil,
               cellType: SaleAfter.ListCell.self,
               detailType: ShoppingOrderDetailCtr.self,
               test: false
        )
        super.viewDidLoad()
        title = "售后".loc
        reload_page()
    }
    open override func layTable(tableView: UIView) {
        jo_contentView.eelay = [
            [tableView,[ee.T.L.B.R]]
        ]
    }
}

open class GoodsOrderListCtr: ASTableContainer {
    
    public var selectIndex = 0
    
    public var isAfter = false{
        didSet{
            if isAfter{
                paginator.net._surl(.售后列表)
                paginator.empty_msg = "没有售后信息".loc
                paginator.parameters = [String:String]()
                paginator.in_test = false
                reload_page()
            }
            else{
                paginator.empty_msg = "没有相关[XX]".replaceVars(values: OrderScheme.default.name)
                paginator.in_test = false
                
            }
        }
    }
    
    open var filterStatus:GoodsOrderStatus = .all{
        didSet{
            paginator.net._surl(.订单列表)
            paginator.parameters = ["status":filterStatus.filterValue]
            reload_page()
        }
    }
    public var menuBar = XLMenuBar()
    
    open override var selector: TP.selector{
        get{
            return [
                "<default>":Goods.OrderListCell.self,
                "cellID<after>":SaleAfter.ListCell.self,
            ]
        }
    }
    
    open override func viewDidLoad() {
        self.content = ASTermsContainer.list(
            net: JoTask().post._surl(.订单列表).log.log_response(),
            node: "list",
            emptyMsg: "没有相关内容".loc,
            params:["status":filterStatus.rawValue],//除了分页参数之外的额外参数
            cellType: Goods.OrderListCell.self,
            detailType: ShoppingOrderDetailCtr.self,
            test: false
        )
        
        
        super.viewDidLoad()
        title = "我的订单".loc
        top_style = .default
        view.backgroundColor = ._background
        
//        reload_page()
        
        weak var wself = self
        __notice.add(task: (Goods.Notice.goodsOrderChange,{_ in
            wself?.reload_page()
        })).add(task: (JoUser.Notice.login,{_ in
            wself?.reload_page()
        })).add(task: (JoUser.Notice.logout,{_ in
            wself?.reload_page()
        }))

        co_delay(delay: 0.1) {[weak self] in
            if let ws = self{
                ws.menuBar.selected(index: ws.selectIndex)
            }
            
        }
        
       
    }
    
    var filters:[Goods.OrderListFilter] = OrderScheme.default.orderListfilters
    
    open override func reload_page() {
        if JoUser.loginUser() == nil{
            menuBar.isUserInteractionEnabled = true
            mo_request_alert(msg: "请登录后查看相关内容".loc, title: "登录提示".loc, bt_title: "登录".loc)
        }else{
            super.reload_page()
        }
    }
    
    
    
    
    public let tableBack:UIView = UIView()
    
    open override var LoadingPresenterBg: UIView{
        get{
            
            return tableBack
        }
    }
    open override func layTable(tableView: UIView) {
        
        
        menuBar.selectedColor = ._tint
        view.eelay = [
            [menuBar,[ee.L.R],[ee.T,TP.topHeight],"40"],
            [tableBack,[menuBar,ee.B,ee.T],[ee.L.B.R]],
        ]
        
        jo_contentView.eelay = [
            [tableView,[ee.T.L.B.R]],
        ]
        
        let list = OrderScheme.default.orderListfilters;
        self.filters = list
        
        let menus = self.filters.map { (filter) -> String in
            return filter.name
        }
        menuBar.loadItems(items: menus)
        weak var wself = self
        menuBar.selectedMenu = {index,name in
            print("+____))))__---\(index)")
            wself?.menuBar.isUserInteractionEnabled = false
            let _filter = list[index]
            switch _filter {
            case .after:
                wself?.isAfter = true
            case .all:
                wself?.isAfter = false
                let status = GoodsOrderStatus.all
                wself?.filterWith(status: status)
            case .status(let status, name: _):
                wself?.isAfter = false
                wself?.filterWith(status: status)
            }
//            if name == "售后"
//            {
//                wself?.isAfter = true
//                
//            }
//            else{
//                wself?.isAfter = false
//                let status = GoodsOrderStatus.statusWith(name: name)
//                wself?.filterWith(status: status)
//            }
//            
        }
    }
    
    open override func load(page: Int, succeed: Bool, list: [Any], response: RootData?, status: PageDataStatus, scrollView: UIScrollView) {
        menuBar.isUserInteractionEnabled = true
        return super.load(page: page, succeed: succeed, list: list, response: response, status: status, scrollView: scrollView)
    }
    
    open func filterWith(status:GoodsOrderStatus) {
        self.filterStatus = status
        switch status {
        case .all:
            paginator.parameters = ["status":status.filterValue]
            reload_page()
        default:
            
            paginator.parameters = ["status":status.filterValue]
            reload_page()
        }
    }
    
    open override func touch(cell: Any, actionID: String, model: NSMutableDictionary) {
        
        switch actionID {
        case "deleteItem":
            let list = DataNode.path(node: "section0", tableNode.model, value: NSMutableArray())
            let row = list.index(of: model)
            let ps = IndexPath(row: row, section: 0)
            
            list.remove(model)
            tableNode.deleteRows(at: [ps], with: .fade)
            
        default:
            break
        }
    }
    
    
    
    open override func tableNode(_ tableNode: ASTableNode, didSelectRowAt indexPath: IndexPath) {
        guard let data = tableNode.cellData(at: indexPath) else {
            return
        }
        if isAfter{
            let ctr = SaleAfterDetailCtr()
            ctr.mo.model = data
            navigationController?.pushViewController(ctr, animated: true)
        }else{
            let ctr = ShoppingOrderDetailCtr()
            ctr.mo.model = data
            navigationController?.pushViewController(ctr, animated: true)
        }
    }
    open override func loadOnePage(list: [Any]) -> [Any] {
        if isAfter{
            for one in list
            {
                if let dict = one as? NSMutableDictionary{
                    dict["cellID"] = "after"
                }
            }
        }
        return list
    }
    
}

extension GoodsOrderListCtr{
    
    
    //    class OrderCell: JoTableCell {
    //        let imgV = UIImageView()
    //        let titleL = UILabel()
    //        let tagsL = UILabel()
    //        let priceL = UILabel()
    //        let numL = UILabel()
    //        override func addLayoutRules() {
    //            contentView.eelay = [
    //                [imgV,[ee.T.L.B,[12,AppTheme.edge,-12]],"50",50],
    //                [titleL,[imgV,ee.R,ee.L,8],[imgV,ee.T,4],[ee.R,-100]],
    //                [tagsL,[imgV,ee.R,ee.L,8],[imgV,ee.B,-4],[ee.R,-100]],
    //                [priceL,[ee.R,-AppTheme.edge],[titleL,ee.Y]],
    //                [numL,[ee.R,-AppTheme.edge],[tagsL,ee.Y]],
    //                [__line,[ee.L.B.R,[0,0.0]],"0.5"]
    //            ]
    //            __line.backgroundColor = FieldCell.separator_color
    //            imgV.img_url = __goods.one_str
    //            titleL.ui.font14.cl_222.text(__goodsNames.one_str)
    //            tagsL.ui.font13.cl_999.text("颜色：黑")
    //            priceL.ui.font13.cl_222.right.text("¥ 120")
    //            numL.ui.font13.cl_999.right.text("X1")
    //        }
    //    }
}


